package web;

import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class UserServlet extends BaseServlet {
    UserService userService=new UserServiceImpl();
    protected void regist(HttpServletRequest req,HttpServletResponse resp)throws  ServletException,IOException{
        System.out.println("regist");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        User user= WebUtils.copyParamToBean(req.getParameterMap(),new User());

//        int sum=Integer.sum(100,100);
        if(token!=null||token.equalsIgnoreCase(code)){
            if(userService.existsUsername(username)){
                System.out.println("用户名["+username+"]已存在!");
                req.setAttribute("msg","用户名已存在");
                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
            }else{
                userService.registerUser(new User(null,username,password,email));
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
            }

        }else{
            req.setAttribute("msg","验证码错误");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            System.out.println("验证码["+code+"]错误");
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }
    }

    protected  void logout(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath());
    }
    protected void login(HttpServletRequest req,HttpServletResponse resp)throws  ServletException,IOException {
        System.out.println("login");
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User loginUser=userService.login(new User(null, username, password, ""));
        if(userService.existsUsername(username)) {
            System.out.println("用户["+username+"]存在");
            if (userService.login(new User(null, username, password, "")) == null) {
                System.out.println("密码错误");
                req.setAttribute("msg","密码错误！");
                req.setAttribute("username",username);
                req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            } else {
                System.out.println("欢迎:" + username);
                req.getSession().setAttribute("user",loginUser);
                req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);


            }
        }
        else{
            req.setAttribute("msg","用户名不存在");
            System.out.println("用户名[" + username+"]不存在");
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }
    }
    protected  void ajaxExistusername(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        String username = request.getParameter("username");
        boolean existsUsername = userService.existsUsername(username);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("existsUsername",existsUsername);
        Gson gson=new Gson();
        String json=gson.toJson(resultMap);
        response.getWriter().write(json);
    }



}
